package com.slipper.bpmn.parser;

import com.slipper.util.Tool;
import org.dom4j.Element;

import com.slipper.bpmn.model.Common;

import java.util.Iterator;

public interface IParser<T extends Common> {
    T parser(Element element);

    default T parser(T t, Element element) {
        t.setCode(element.attributeValue("id"));
        t.setName(element.attributeValue("name"));
        t.setType(element.getName());

        String ordinal = element.attributeValue("ordinal");
        if (Tool.isNotBlank(ordinal)) {
            t.setOrdinal(Integer.parseInt(ordinal));
        }

        Iterator<Element> iter = element.elementIterator();
        while(iter.hasNext()) {
            Element e = iter.next();
            if ("incoming".equals(e.getName())) {
                t.addInLine(e.getText());
            } else if ("outgoing".equals(e.getName())) {
                t.addOutLine(e.getText());
            }
        }
        return t;
    }
}
